package com.cdshangpai.utils.offlinecache;

import android.util.Log;

import com.cdshangpai.photoapp.LogConfig;
import com.cdshangpai.utils.api.Api;
import com.cdshangpai.utils.log.LogUtil;
import com.cdshangpai.utils.offlinecache.aboutus.AboutusCacheProcessor;
import com.cdshangpai.utils.offlinecache.account.AccountCacheProcessor;
import com.cdshangpai.utils.offlinecache.casecache.CaseCacheProcessor;
import com.cdshangpai.utils.offlinecache.hotal.HotalCacheProcessor;
import com.cdshangpai.utils.offlinecache.videocache.VideoCacheProcess;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.locks.ReentrantLock;

public class CacheManager {
    long loopTimeLength = 1000 * 60*60;
    long shotTimeLength = 1000 * 60/6;
    CaseCacheProcessor caseCacheProcessor;
    HotalCacheProcessor hotalCacheProcessor;
    VideoCacheProcess videoCacheProcess;
    AccountCacheProcessor accountCacheProcessor;
    AboutusCacheProcessor aboutusCacheProcessor;
    Api api;

    static BlockingQueue<Integer> continueQueue;
    static ReentrantLock lock = new ReentrantLock();

    public static Boolean updateing=false;

    public CacheManager() {
        continueQueue = new LinkedBlockingDeque<>();
        api = new Api();
        caseCacheProcessor = new CaseCacheProcessor();
        hotalCacheProcessor = new HotalCacheProcessor();
        videoCacheProcess = new VideoCacheProcess();
        accountCacheProcessor = new AccountCacheProcessor();
        aboutusCacheProcessor = new AboutusCacheProcessor();
    }

    /**
     * 本地缓存任务系统级入口。app启动时同时启动,
     */
    public void beginTask() {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    Log.i(LogConfig.logTag, "begin next...");
                    boolean success = begin();
                    Log.i(LogConfig.logTag,"end cache...");
                    delay(success);
                    try {
                        Integer next = continueQueue.take();
                    } catch (Exception e) {
                    }
                }
            }
        }.start();
    }


    public void delay(boolean success) {
        new Thread() {
            @Override
            public void run() {
                long timeLength = success ? loopTimeLength : shotTimeLength;
                try {
                    Thread.sleep(timeLength);
                    next();
                } catch (Exception e) {
                }
            }
        }.start();
    }

    public static void next() {
        lock.lock();
        int size=continueQueue.size();
        Log.i(LogConfig.logTag,"queue size:"+size);
        if(size!=0)
        {
            lock.unlock();
            return;
        }
        try {
            continueQueue.put(1);
        } catch (Exception e) {
        }
        lock.unlock();
    }

    /**
     * 服务器数据缓存本地工作线程入口
     */
    private boolean begin() {
        LogUtil.save("开始同步");
        Boolean result=true;
        updateing=true;
        VersionBean versionBean = downloadVersion();
        if (versionBean == null) {
            LogUtil.save("完成同步，版本获取失败");
            return false;
        }
        if (!accountCacheProcessor.begin(versionBean.accounts)) {
            result=false;
//            return false;
        }
        if(versionBean.aboutus!=null) {
            if (!aboutusCacheProcessor.begin(versionBean.aboutus)) {
                result=false;
//                return false;
            }
        }
        if (!caseCacheProcessor.begin(versionBean.cases)) {
            result=false;
//            return false;
        }
        if (!hotalCacheProcessor.begin(versionBean.holtals)) {
            result=false;
//            return false;
        }
        if (!videoCacheProcess.begin(versionBean.videos)) {
            result=false;
//            return false;
        }
        LogUtil.save("成功完成服务器同步,结果:"+(result?"成功":"失败"));
        Log.i(LogConfig.logTag, "成功完成服务器同步,结果->"+result);
        updateing=false;
        return result;
    }


    /**
     * 下载最新服务器版本数据
     *
     * @return
     */
    private VersionBean downloadVersion() {
        VersionBean versionBean = new VersionBean();
        versionBean = api.queryDbVersion();
        return versionBean;
    }
}
